package com.qingxin.wuye.repertory.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.repertory.entity.ResType;


@Service
public class ResTypeService extends BaseService<ResType,String>{
	
	
	
	@SuppressWarnings("unchecked")
	public List<JsTreeNode> getUITreeData(LoginUser loginUser, boolean isParent){
		List<JsTreeNode> list = new ArrayList<>();
		if(loginUser == null){
			return list;
		}
		StringBuilder hql = new StringBuilder();
		hql.append("from ResType where 1=1 ");
		hql.append(" order by typeName asc");
		List<ResType> types = listByHql(hql.toString());
		if(types != null){
			for(ResType type : types){
				list.add(new JsTreeNode(type.getTypeid(), type.getTypeName(), SystemCfg.UI_DATATYPE_CITY, "typeid", isParent));
			}
		}
		return list;
	}

	/**
	 * 列表数据
	 * @param searchname
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String searchname, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("from ResType");
		if(StringUtils.isNotEmpty(searchname)){
			searchname = searchname.replace("'", "");
			querier.append(" and searchname like :searchname ");
			querier.setParam("searchname", "%"+searchname+"%");
		}
		querier.append(pageParam.getOrderby(" order by createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
		
	}
	
	/**
	 * 添加类别
	 * @param resType
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(ResType resType, LoginUser loginUser, HttpServletRequest request){
		resType.setCreateTime(new Date());
		resType.setCreateBy(loginUser.getLogin_name());
		this.save(resType);
		return null;
	}
	
	/**
	 * 更新
	 * @param resType
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(ResType resType, LoginUser loginUser, HttpServletRequest request){
		ResType old = getSafeEntity(resType.getTypeid());
		String fields = "typeName,typeCode";
		BeanUtil.setTargetFields(old, resType, fields, true);
		Updater<ResType> updater = new Updater<>(old, fields);
		this.update(updater);
		return null;
	}
	
	/**
	 * 删除
	 * @param typeids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] typeids, LoginUser loginUser, HttpServletRequest request){
		for(String typeid : typeids){
			ResType resType = get(typeid);
			if(resType==null){
				continue;
			}
			this.delete(resType);
		}
		
		return null;
	}
	
	
	/**
	 * 判断指定名称是否被使用
	 * @param companyName
	 * @param companyid
	 * @return
	 */
	public boolean isNameDuplicate(String typeName, String typeid){
		if(StringUtils.isEmpty(typeid)){
			Object obj = getByHql("select typeid from ResType where typeName=? ", typeName);
			return obj!=null;
		}else {
			Object obj = getByHql("select typeid from ResType where typeid<>? and typeName=? ", typeid, typeName);
			return obj!=null;
		}
	}
	
	
	/**
	 * 判断指定代码是否被使用
	 * @param typecode
	 * @param typeid
	 * @return
	 */
	public boolean isCodeDuplicate(String typeCode, String typeid){
		if(StringUtils.isEmpty(typeid)){
			Object obj = getByHql("select typeid from ResType where typeCode=? ", typeCode);
			return obj!=null;
		}else {
			Object obj = getByHql("select typeid from ResType where typeid<>? and typeCode=? ", typeid, typeCode);
			return obj!=null;
		}
	}
}
